package flc.ast.utils;


import android.view.View;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import flc.ast.bean.MyAlbumBean;
import flc.ast.fragment.HomeFragment;


public class CardLayoutManagerLan extends RecyclerView.LayoutManager {

    RecyclerView rv;

    public CardLayoutManagerLan(RecyclerView rv) {
        this.rv = rv;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    }

    /**
     * 自定义LayoutManager核心是摆放控件，所以onLayoutChildren方法是我们要改写的核心
     *
     * @param recycler
     * @param state
     */
    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        //缓存
        detachAndScrapAttachedViews(recycler);

        //获取所有item(包括不可见的)个数
//        int count = getItemCount();
//        //由于我们是倒序摆放，所以初始索引从后面开始
//        int initIndex = count - CardConfig.SHOW_MAX_COUNT;
//        if (initIndex < 0) {
//            initIndex = 0;
//        }
//        int initIndex=count-1;
        //2)只将最上面CardConfig.SHOW_MAX_COUNT个view添加到RecylerView容器里面
        int itemCount=getItemCount();
        int bottomPosition;
        if(itemCount< CardConfig.SHOW_MAX_COUNT){
            bottomPosition=0;

        }else{
            bottomPosition=itemCount-CardConfig.SHOW_MAX_COUNT;
        }

        float rvTranX=0f;
        if(allViewsIsRight(recycler)){
            rvTranX=-CardConfig.TRANSLATION_X;
        }else if(allViewsIsLeft(recycler)){
            rvTranX=CardConfig.TRANSLATION_X;
        }else{
            rvTranX=0f;
        }
        rv.setTranslationX(rvTranX);

        for (int i = bottomPosition; i < itemCount; i++) {
            //从缓存中获取view
            View view = recycler.getViewForPosition(i);
            //添加到recyclerView
            addView(view);
            //测量一下view
            measureChild(view, 0, 0);

            //居中摆放，getDecoratedMeasuredWidth方法是获取带分割线的宽度，比直接使用view.getWidth()精确
            int realWidth = getDecoratedMeasuredWidth(view);
            int realHeight = getDecoratedMeasuredHeight(view);
            int widthPadding = (int) ((getWidth() - realWidth) / 2f);
            int heightPadding = (int) ((getHeight() - realHeight) / 2f);

            //摆放child
            layoutDecorated(view, widthPadding, heightPadding,
                    widthPadding + realWidth, heightPadding + realHeight);
            //根据索引，来位移和缩放child
            int level = itemCount - i - 1;
            //level范围（CardConfig.SHOW_MAX_COUNT-1）- 0
            // 最下层的不动和最后第二层重叠
            if (level == CardConfig.SHOW_MAX_COUNT - 1) {
                level--;
            }
            MyAlbumBean bean= (MyAlbumBean) view.getTag();
            if( level!=0 && bean.getDirection() == ItemTouchHelper.LEFT){//在左侧
                view.setTranslationX(level * - CardConfig.TRANSLATION_X);
            }else if(level!=0 && bean.getDirection() == ItemTouchHelper.RIGHT){//在右侧
                view.setTranslationX(level * CardConfig.TRANSLATION_X);
            }else{
//                view.setTranslationX(level * -CardConfig.TRANSLATION_X);
            }
            view.setScaleX(1 - level * CardConfig.SCALE);
            view.setScaleY(1 - level * CardConfig.SCALE);
        }
    }

    private boolean allViewsIsRight(RecyclerView.Recycler recycler) {
        for (int i = 0; i < getItemCount()-1; i++) {
            View view = recycler.getViewForPosition(i);
           MyAlbumBean bean= (MyAlbumBean) view.getTag();
            if(bean.getDirection()==ItemTouchHelper.LEFT){
                return false;
            }
        }
        return true;
    }

    private boolean allViewsIsLeft(RecyclerView.Recycler recycler) {
        for (int i = 0; i < getItemCount()-1; i++) {
            View view = recycler.getViewForPosition(i);
            MyAlbumBean bean= (MyAlbumBean) view.getTag();
            if(bean.getDirection()==ItemTouchHelper.RIGHT){
                return false;
            }
        }
        return true;
    }
}
